import Util from '../libs/util'
import Api from '../libs/apiConfig'

export default {
    state: {
        register:{
            status:'',
            upFile:{
                'zzrFileName':{},
                'frFileName':{}
            }
        },
        login:{

        },
        authCode:{},
        search:{
        },
    },
    getters: {
    },
    mutations: {
        SET_AUTHCODE(state,data){
            state.authCode=data
        },
        SET_registerpage_register_upfile(state,data){
            state.register.upFile[data.fileName]=data.file
        },
        SET_registerpage_register_status(state,data){
            state.register.status = data
        }
    },
    actions: {
        loginPage_login({commit},paramsObj) {
            return new Promise((resolve, reject) => {
                Util.ajax.post(Api.userLogin,paramsObj).then((response) => {
                    if(response.data.resultObj.code==1000){
                        // commit('SET_LOGIN',{
                        //     data:response.data
                        // })

                        //保存登陆信息到cookie
                        Util.setCookie('login',response.data.frontUser)
                        resolve(response.data.frontUser)
                    }else{
                        reject(response.data.resultObj)
                    }
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        registerPage_upfile({commit},paramsObj) {
            commit('SET_registerpage_register_upfile',paramsObj)
        },
        registerPage_register({commit},paramsObj) {
            commit('SET_registerpage_register_status','loading')


            return new Promise((resolve, reject) => {
                setTimeout(function () {
                    commit('SET_registerpage_register_status','success')
                    resolve();
                }, 3000);
            })
        },
        findPassPage_findPass({commit},paramsObj) {
            return new Promise((resolve, reject) => {
                Util.ajax.post(Api.forgetPassword,paramsObj).then((response) => {
                    if(response.data.resultObj.code==1000){
                        resolve(response.data)
                    }else{
                        reject(response.data)
                    }
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        commonPage_checkUsername({commit},paramsObj) {
            return new Promise((resolve, reject) => {
                Util.ajax.post(Api.checkUsername,paramsObj).then((response) => {
                    // if(response.data.status=='y'){
                    //     resolve(response.data)
                    // }else{
                    //     reject(response.data)
                    // }
                    resolve(response.data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        commonPage_sendAuthCode({commit},paramsObj) {
            return new Promise((resolve, reject) => {
                Util.ajax.post(Api.authCode,paramsObj).then((response) => {
                    if(response.data.resultObj.code==1000){
                        commit('SET_AUTHCODE',{
                            data:response.data
                        })
                        resolve(response.data)

                        //更新cookie的authCode
                        let frontUser = Util.getCookie('login',response.data.frontUser) || {}
                        if(frontUser.authCode){
                            frontUser.authCode = response.data.frontUser.authCode
                            Util.setCookie('login',frontUser)
                        }
                    }else{
                        reject(response.data)
                    }
                }).catch((err) => {
                    reject(err)
                })
            })
        }
    }
};
